﻿using BacklogParser.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace BacklogParser.Parsers
{
    public static class CitationReferencesParser
    {
        public static List<CitationReference> Parse(List<XElement> citationReferences)
        {
            var result = new List<CitationReference>();

            foreach (var item in citationReferences)
            {
                var citations = item.Descendants(Constants.Epo + "citation").ToList();
                var citationList = new List<Citation>();
                foreach (var citation in citations)
                {
                    var citationInstance = new Citation
                    {
                        Id = citation.Attribute("id").Value,
                        CitedIn = citation.Attribute("cited-in").Value,
                        Office = citation.Attribute("office").Value
                    };

                    var categories = citation.Descendants(Constants.Epo + "category").ToList().Select(x => x.Value);
                    citationInstance.Category = string.Join(", ", categories);

                    var doi = citation.Descendants(Constants.Epo + "doi").FirstOrDefault();
                    if(doi != null)
                    {
                        citationInstance.Doi = doi.Value;
                    }

                    var npcit = citation.Descendants(Constants.Epo + "npcit").FirstOrDefault();
                    if(npcit != null)
                    {
                        citationInstance.Npcit = npcit.Value;
                    }

                    var patcit = citation.Descendants(Constants.Epo + "patcit").FirstOrDefault();
                    if(patcit != null)
                    {
                        var documentIds = patcit.Descendants(Constants.Epo + "document-id").ToList();
                        foreach(var document in documentIds)
                        {
                            citationInstance.DocumentCountry += document.Descendants(Constants.Epo + "country").First().Value;
                            citationInstance.DocumentNumber += document.Descendants(Constants.Epo + "doc-number").First().Value;
                        }
                    }

                    citationList.Add(citationInstance);


                }
                result.Add(new CitationReference { Citations = citationList });
            }

            return result;
        }
    }

    public class Citation
    {
        public string Id { get; set; }

        public string Office { get; set; }

        public string CitedIn { get; set; }

        public string Doi { get; set; }

        public string Category { get; set; }

        public string Npcit {get; set;}

        public string DocumentNumber { get; set; }

        public string DocumentCountry { get; set; }

    }
}
